A few comments about this example: the sizeof operator in ANSI C produces a value whose type is size_t. However in Think C 4.0 its type is simply int. Here this does not cause difficulties since the prototype for malloc() enforces the necessary type conversion. Also note that a cast operator* is used to convert the void pointer returned by malloc to the desired type. This is not strictly necessary since automatic type conversion will take place upon assignment.
Dynamic memory allocation is often used when the amount of memory needed by an application is not known before run-time. The program may define a large array of pointers to data structures, but dynamically allocate the individual data structures pointed to by elements of the array only as needed:
struct personnel_rec *person_array[1000]; /* doesn't yet allocate any
. personnel_rec's */
.
person_array[i] = malloc(sizeof (struct personnel_rec)); /* allocate ith element */